Skip to content

Avoid integer overflow in find_peaks - #664

Merged
tompollard merged 3 commits into
MIT-LCP:mainfrom
Kulraj69:fix/integer-find-peaks
Nov 23, 2026
Merged

tompollard merged 3 commits into
MIT-LCP:mainfrom
Kulraj69:fix/integer-find-peaks

Conversation

@Kulraj69

Copy link
Copy Markdown
Contributor

Problem

find_peaks subtracts adjacent samples before classifying slope signs. For signed integer arrays, that subtraction can overflow and drop hard or plateau peaks. For unsigned integer arrays, assigning -1 back into the unsigned temporary raises OverflowError with NumPy 2.

This is reachable with digital samples returned by rdrecord(..., physical=False, return_res=16), not only synthetic arrays.

Change

For integer input only, compare adjacent samples directly and store the resulting slope signs in int8. This avoids subtraction overflow without converting uint64 samples to floating point. The existing floating-point subtraction path is unchanged.

Add regression cases for signed and unsigned limits, plateaus, closely spaced uint64 values, and a wrsamp / digital rdrecord round trip. The round-trip fixture uses valid format-16 samples, not its missing-value sentinel. Add a changelog fragment.

Validation

Local Python 3.12.16 / NumPy 2.5.5:

  • Before the fix: the focused selection had 10 failures and 2 passing existing controls.
  • After the fix: the same selection passed, 12 passed and 122 deselected.
  • On current upstream main and again after downloading this published branch: pytest tests/test_processing.py -k 'find_peaks or find_local_peaks' -p no:cacheprovider -q, 86 passed and 48 deselected.
  • Differential checks against an integer-safe reference: 800 integer cases and 300 floating-point cases passed.
  • Repository-pinned Ruff 0.17.3 check and format-check passed for both changed Python files.

The full suite, documentation build, a second NumPy version, and upstream CI have not been run locally. This is not a clinical-validation claim.

AI assistance was used for the implementation, regression tests, and this description. The reported checks were executed locally; no independent human review is claimed.

Compare adjacent integer samples directly, keeping the existing floating-point path unchanged.
Cover signed and unsigned dtype limits, plateaus, uint64 precision, and a real digital-record round trip.
Add changelog fragment for direct integer-sample comparisons.

@tompollard tompollard left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Kulraj69, this is a clear fix with good tests. Comparing neighbouring samples instead of subtracting them avoids the overflow, and keeping floats on the old path keeps their behaviour with NaN unchanged.

I checked that the new tests fail without the fix (10 of 12) and pass with it, and that results are unchanged where the old code did not overflow: random small-integer and float signals, and record 100 in digital units, give the same peaks as on main.

One optional suggestion: on the integer path, tmp already holds -1, 0 or 1, so the three tmp[np.where(...)] = ... lines after it only matter for floats. Moving them into the else branch, or using tmp = np.sign(sig - tmp) there, would make that clearer. Happy to merge as it is.

@tompollard
tompollard merged commit 16e5a48 into MIT-LCP:main Nov 23, 2026
18 checks passed
tompollard added a commit that referenced this pull request Nov 25, 2026
## Problem

`processing.find_local_peaks` sends integer signals through
`scipy.ndimage.maximum_filter1d`, which rounds large 64-bit integers
internally. Small differences on a large positive or negative offset can
disappear, producing false or missing peaks.

For example, an `int64` signal formed by adding `2**60` to `[0, 1, 0]`
should retain its single peak at index 1. The same issue affects
`uint64` values near `2**63` and negative `int64` offsets.

## Change

Use the existing exact sample-by-sample scanning path when an integer
signal contains values outside the exact integer range of a double.
Ordinary integer and floating-point inputs retain their existing path.
The constant-signal early return is unchanged.

This deliberately trades speed for exact comparisons on those
large-integer inputs; no performance improvement is claimed for them. It
does not alter the vectorized common-input path introduced in #655 or
the separate `find_peaks` overflow fix in #664.

Add 18 regression cases spanning positive and negative `int64` offsets,
`uint64`, two radii and three signal shapes, including a plateau.
Expected peaks come from explicit bounded windows over the small, exact
shape values. Four small-value dtype controls and input-immutability
checks are included, plus a changelog fragment.

## Validation

Python 3.12.16, isolated against upstream
`98df2c9fba00943004ceac42677744f85a98e702`:

- Unpatched regression file: 18 failed, 4 passed.
- Exact files downloaded from the proposed branch, regression file plus
`tests/test_processing.py` and `tests/test_hr.py`, excluding network
tests: 238 passed.
- Pinned Ruff 0.17.3 check and format passed for both changed Python
files.
- Strict Sphinx 9.0.4 HTML build (`-W --keep-going -E`) passed.

Other private fixes were not included. Full-repository tests,
performance benchmarks, other Python/dependency versions and remote CI
were not run for this change. Prepared and tested with AI assistance; no
independent human review is claimed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants